package com.syn.synold4s.vm;

import android.app.Application;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import com.gaoyi.basemodule.apiresult.ApiResultData;
import com.gaoyi.basemodule.baseCallback.OnResultListener;
import com.gaoyi.basemodule.baseUtil.LanguageUtils;
import com.gaoyi.basemodule.baseUtil.OKHttpUtil;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseUtil.ThreadManager;
import com.gaoyi.basemodule.baseUtil.ToastUtil;
import com.gaoyi.basemodule.baseconstant.ApiStatus;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.gaoyi.basemodule.basevm.BaseViewModel;
import com.google.gson.reflect.TypeToken;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.GoogleMapBean;
import com.syn.synold4s.bean.dto.MapAllDeviceDto;
import com.syn.synold4s.bean.dto.ShortDto;
import com.syn.synold4s.bean.dto.UpdateFilterDto;
import com.syn.synold4s.repository.ComboRepository;
import com.syn.synold4s.repository.GoogleMapRepository;
import com.syn.synold4s.repository.MainRepository;
import java.util.ArrayList;
import java.util.List;

public class GoogleMapViewModel extends BaseViewModel {

    final MainRepository mRepository;
    private final GoogleMapRepository googleMapRepository;
    private MutableLiveData<MapAllDeviceDto> mapDeviceDetails;
    MutableLiveData<List<MapAllDeviceDto>> mMapDevice;
    boolean mapWatch = false;
    boolean mapLock = false;
    boolean map360 = false;
    boolean mapTowerCrane = false;
    MutableLiveData<List<ShortDto>> mMapTypes;
    final ComboRepository mComboRepository;
    MutableLiveData<String> mAddress;

    public void setMapWatch(boolean mapWatch) {
        this.mapWatch = mapWatch;
    }

    public void setMapLock(boolean mapLock) {
        this.mapLock = mapLock;
    }

    public void setMap360(boolean map360) {
        this.map360 = map360;
    }

    public void setMapTowerCrane(boolean mapTowerCrane) {
        this.mapTowerCrane = mapTowerCrane;
    }

    public GoogleMapViewModel(@NonNull Application application) {
        super(application);
        googleMapRepository = new GoogleMapRepository();
        mRepository = new MainRepository();
        mComboRepository = new ComboRepository();
    }

    public MutableLiveData<List<ShortDto>> getMapTypes() {
        if (mMapTypes == null) {
            mMapTypes = new MutableLiveData<>();
            mMapTypes.setValue(new ArrayList<>());
        }
        return mMapTypes;
    }

    public MutableLiveData<String> getAddress() {
        if (mAddress == null) {
            mAddress = new MutableLiveData<>();
        }
        return mAddress;
    }

    public MutableLiveData<MapAllDeviceDto> getMapDeviceDetails() {
        if (mapDeviceDetails == null) {
            mapDeviceDetails = new MutableLiveData<>();
        }
        return mapDeviceDetails;
    }

    public MutableLiveData<List<MapAllDeviceDto>> getMapDevice() {
        if (mMapDevice == null) {
            mMapDevice = new MutableLiveData<>();
            mMapDevice.setValue(new ArrayList<>());
        }
        return mMapDevice;
    }

    private boolean mIsGetMainData = false;

    public void getData(String taCode) {
        if (mIsGetMainData) {
            return;
        }
        mIsGetMainData = true;
        ThreadManager.getInstance().execute(() -> getAllMapDevices(taCode));
        mIsGetMainData = false;
    }

    public void getDeviceDetails(String tagCode, String id) {
        if (getView() != null) {
            getView().showLoading();
        }
        googleMapRepository.deviceDetails(tagCode, id, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<MapAllDeviceDto> apiResultData = (ApiResultData<MapAllDeviceDto>) apiResult;
                if (apiResultData.getData() != null && apiResultData.getStatus() == ApiStatus.SUCCESS) {
                    mapDeviceDetails.setValue(apiResultData.getData());
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<MapAllDeviceDto>>() {
        }.getType());
    }

    public void getTowerDetails(String id, String tagCode) {
        if (getView() != null) {
            getView().showLoading();
        }
        googleMapRepository.towerDetails(id, tagCode, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<MapAllDeviceDto> apiResultData = (ApiResultData<MapAllDeviceDto>) apiResult;
                if (apiResultData.getData() != null && apiResultData.getStatus() == ApiStatus.SUCCESS) {
                    mapDeviceDetails.setValue(apiResultData.getData());
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<MapAllDeviceDto>>() {
        }.getType());
    }

    public void changeMapFilter(String ids) {
        changeFilter(ids, "SynMap");
    }

    private void changeFilter(String ids, String type) {
        UpdateFilterDto bean = new UpdateFilterDto();
        bean.setFilterType(type);
        bean.setProjCode(SPUtil.getInstance().getString(SPName.PROJ_CODE));
        bean.setSynProjIds(ids);
        mRepository.changeFilter(bean, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {

            }

            @Override
            public void onFails(Exception e) {

            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void getMapType() {
        ThreadManager.getInstance().execute(() -> {
            List<ShortDto> maps = mComboRepository.comboSync("synProjMaps");
            if (maps != null) {
                OKHttpUtil.getInstance().post(() -> mMapTypes.setValue(maps));
            }
        });
    }

    private void getAllMapDevices(String tagCode) {
        List<MapAllDeviceDto> allDeviceDtos = mRepository.allMapDevices(tagCode);
        if (allDeviceDtos != null) {
            OKHttpUtil.getInstance().post(() -> mMapDevice.setValue(allDeviceDtos));
        }
    }

    public void getAddressTrans(double lat, double lng) {
        ThreadManager.getInstance().execute(() -> {
            GoogleMapBean bean = googleMapRepository.getAddress(lat, lng, LanguageUtils.getInstance().getMapLanguage());
            if (bean != null && bean.getResults() != null && !bean.getResults().isEmpty()) {
                OKHttpUtil.getInstance().post(() -> {
                    List<GoogleMapBean.ResultsBean> results = bean.getResults();
                    String compoundCode = "";
                    if (!results.isEmpty()) {
                        compoundCode = bean.getResults().get(0).getFormatted_address();
                    }
                    if (!TextUtils.isEmpty(compoundCode)) {
                        mAddress.setValue(compoundCode);
                    }
                });
            }
        });
    }
}
